import {
    _decorator,
    Component,
    Node,
    Vec3,
    Quat,
    math,
    input,
    Input,
    EventKeyboard,
    KeyCode,
    director,
} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CharacterController')
export class CharacterController extends Component {
    @property(Node)
    cameraNode: Node | null = null; // 摄像机节点

    @property
    moveSpeed: number = 5; // 移动速度

    @property
    isFlying: boolean = false; // 是否为飞行状态

    private moveDirection: Vec3 = new Vec3(); // 移动方向
    private velocity: Vec3 = new Vec3(); // 当前速度
    private gravity: number = -10; // 重力加速度
    private jumpForce: number = 5; // 跳跃力

    onLoad() {
        // 监听键盘输入
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

    onDestroy() {
        // 移除键盘监听
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

    private onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                this.moveDirection.z = 1;
                break;
            case KeyCode.KEY_S:
                this.moveDirection.z = -1;
                break;
            case KeyCode.KEY_A:
                this.moveDirection.x = -1;
                break;
            case KeyCode.KEY_D:
                this.moveDirection.x = 1;
                break;
            case KeyCode.SPACE:
                if (!this.isFlying) {
                    this.velocity.y = this.jumpForce; // 跳跃
                }
                break;
        }
    }

    private onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
            case KeyCode.KEY_S:
                this.moveDirection.z = 0;
                break;
            case KeyCode.KEY_A:
            case KeyCode.KEY_D:
                this.moveDirection.x = 0;
                break;
        }
    }

    update(deltaTime: number) {
        if (!this.cameraNode) return;

        // 获取摄像机的前方和右方向
        const cameraForward = this.cameraNode.forward.clone();
        const cameraRight = this.cameraNode.right.clone();

        // 将 Y 分量置零（如果是地面移动）
        if (!this.isFlying) {
            cameraForward.y = 0;
            cameraRight.y = 0;
            cameraForward.normalize();
            cameraRight.normalize();
        }

        // 计算移动方向（相对于摄像机方向）
        const moveX =
            this.moveDirection.x * cameraRight.x +
            this.moveDirection.z * cameraForward.x;
        const moveY = this.isFlying
            ? this.moveDirection.x * cameraRight.y +
              this.moveDirection.z * cameraForward.y
            : 0;
        const moveZ =
            this.moveDirection.x * cameraRight.z +
            this.moveDirection.z * cameraForward.z;

        // 计算移动速度
        this.velocity.x = moveX * this.moveSpeed;
        this.velocity.z = moveZ * this.moveSpeed;

        // 如果不是飞行状态，应用重力
        if (!this.isFlying) {
            this.velocity.y += this.gravity * deltaTime;
        } else {
            this.velocity.y = moveY * this.moveSpeed;
        }

        // 更新角色位置
        const displacement = this.velocity
            .clone()
            .multiplyScalar(deltaTime);
        this.node.translate(displacement, Node.NodeSpace.WORLD);

        // 如果角色在地面，且 Y 速度向下，则停止下落
        if (!this.isFlying && this.node.worldPosition.y <= 0) {
            // this.node.setPosition(this.node.position.x, 0, this.node.position.z);
            this.velocity.y = 0;
        }
    }
}
